﻿using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Entitys.MainTasks;
using HamcoWcs.Entitys.SubTasks;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.EdgeInfos;
using HamcoWcs.Models.MainTasks;

namespace HamcoWcs.Contract.ContractMainTask.IMainTaskParsers
{
    /// <summary>
    /// 子任务解析器
    /// </summary>
    public interface ISubTaskParser
    {
        /// <summary>
        /// 子任务类型
        /// </summary>
        EnumSubTaskType SubTaskType { get; }
        /// <summary>
        /// 处理子任务
        /// </summary>
        /// <param name="mainTask">主任务</param>
        /// <param name="edge">分段路径</param>

        void Deal(MainTask mainTask, EdgeInfoDto edge);
    }

    /// <summary>
    /// 解析器基类，继承自<see cref="ISubTaskParser"/><br/>
    /// 每个解析器需要指定其类型，<see cref="EnumSubTaskType"/>
    /// </summary>
    public abstract class SubTaskParserBase : ISubTaskParser
    {
        protected readonly ILocationService _locationService;

        /// <summary>
        /// 基础解析器构造函数
        /// </summary>
        /// <param name="locationService">货架服务</param>
        protected SubTaskParserBase(ILocationService locationService)
        {
            _locationService = locationService;
        }

        public abstract EnumSubTaskType SubTaskType { get; }

        public abstract void Deal(MainTask mainTask, EdgeInfoDto edge);

        /// <summary>
        /// 创建子任务
        /// </summary>
        /// <param name="mainTask">主任务</param>
        /// <param name="from">起点</param>
        /// <param name="to">终点</param>
        /// <param name="excuteName">执行器名称</param>
        /// <returns></returns>
        protected SubTask CreateNewSubTask(MainTask mainTask, string from, string to, string excuteName)
        => new SubTask()
        {
            TaskFrom = mainTask.Sender,
            SubTaskId = "",
            SubTaskType = SubTaskType.ToString(),
            TaskStatus = EnumSubTaskStatus.NewTask.ToString(),
            UnitId = mainTask.UnitId,
            TaskSeq = mainTask.SubTasks.Count + 1,
            FromLoc = from,
            ToLoc = to,
            PlcName = excuteName
        };


        /// <summary>
        /// 创建子任务
        /// </summary>
        /// <param name="mainTask">主任务</param>
        /// <param name="extend">扩展项</param>
        /// <param name="excuteName">执行器名称</param>
        /// <returns></returns>
        protected SubTask CreateNewSubTask(MainTask mainTask, ExtendDto extend, string excuteName)
            => CreateNewSubTask(mainTask, extend.FromAddress.PositionName, extend.ToAddress.PositionName, excuteName);


        /// <summary>
        /// 创建子任务
        /// </summary>
        /// <param name="mainTask">主任务</param>
        /// <param name="edge">分段路径</param>
        /// <returns></returns>
        protected SubTask CreateNewSubTask(MainTask mainTask, EdgeInfoDto edge)
            => CreateNewSubTask(mainTask, edge.FromNode, edge.ToNode, edge.ExcutePlc);
    }
}
